package com.medical.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.system.mapper.SysRoleMapper;
import com.medical.system.mapper.SysUserRoleMapper;
import com.medical.system.model.SysRole;
import com.medical.system.model.SysUserRole;
import com.medical.system.service.SysRoleService;
import com.medical.system.vo.AssginRoleVo;
import com.medical.system.vo.SysRoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * @ 方法描述 : 分页查询+模糊查询
     * @ 作者 : 风花雪月
     * @ 时间 : 2024/8/2 9:31
     * @ 参数 : [p1, sysRoleQueryVo]
     * @return: IPage<SysRole>
     **/
    @Override
    public IPage<SysRole> selectPage(IPage<SysRole> p1, SysRoleQueryVo sysRoleQueryVo) {
        IPage<SysRole> sysRoleIPage = this.baseMapper.selectPage(p1, sysRoleQueryVo);
        return sysRoleIPage;
    }

    /**
     * @ 方法描述 : 根据用户ID获取角色
     * @ 作者 : 风花雪月
     * @ 时间 : 2024/8/2 9:32
     * @ 参数 : [userId]
     * @return: Map<Object>
     **/
    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {

        //1.获取所有的角色
        List<SysRole> roles = this.baseMapper.selectList(null);
        //2.根据用户id 去查询当前已经分配的角色
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<SysUserRole> userRoles = this.sysUserRoleMapper.selectList(queryWrapper);

        //3.从userRoles 中获取到 roleId
        List<Long> list = new ArrayList<>();
        for (SysUserRole userRole : userRoles) {
            Long roleId = userRole.getRoleId();
            list.add(roleId);
        }
        //创建返回的Map
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles", roles);
        returnMap.put("userRoleIds", list);
        return returnMap;
    }

    //    角色分配
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //1.根据用户 id 删除原来分配的角色
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", assginRoleVo.getUserId());
        this.sysUserRoleMapper.delete(queryWrapper);
        //2.获取所有的角色id
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            //3.保存
            sysUserRoleMapper.insert(sysUserRole);
        }
    }
}
